/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.pedagogue.examination;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKey;
import javax.persistence.MapKeyColumn;

/**
 *
 * @author abin
 */
@Entity
public class GradingSystem implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    @MapKeyColumn(name="GRADE_NAME")
    @JoinColumn
    private Map<String, Interval> grade = new HashMap<String, Interval>();
   

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    public void addGrade(String gradeName,float lower,float upper,boolean isLowerIncluded, boolean isUpperIncluded){
       if(!isValidGradeRange(lower, upper,true,true)){
            throw  new IllegalArgumentException("Grade range is not valid, this range may alredy existed");
       }
        if(grade==null){
            grade=new  HashMap<String, Interval>();
        }
        grade.put(gradeName, new Interval(lower, upper, true, true));
    }
    
    public String evaluateResult(float percentage){
        
        return "";
    }
    public boolean  havePassMark(float percentage){
        return true;
    }
    private boolean isValidGradeRange(float lower,float upper,boolean isLowerIncluded, boolean isUpperIncluded){
        //TODO validate if range alredy added
        if(lower<0 || upper>100||lower>upper)
            throw new IllegalArgumentException("Lower "+lower+" and Upper"+upper+" is not valid range");
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof GradingSystem)) {
            return false;
        }
        GradingSystem other = (GradingSystem) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.letmeshare.pedagogue.examination.Grade[ id=" + id + " name="+name+" ]";
    }
    
}
